import json

from rest_framework.response import Response
from rest_framework.views import APIView

from six_it.models import Recruit
from xiong_tao169.serializer.enterprise_recruit import RecruitSerializer


class SearchView(APIView):
    '''搜索职位'''

    def post(self, request):

        dict = json.loads(request.body)
        cityname = dict.get('cityname')
        keyword = dict.get('keyword')
        if cityname and keyword:
            recruit = Recruit.objects.filter(state="1", city=cityname, jobname__icontains=keyword).order_by(
                "-createtime")

        elif cityname:
            recruit = Recruit.objects.filter(state="1", city=cityname).order_by("-createtime")

        else:
            recruit = Recruit.objects.filter(state="1", jobname__icontains=keyword).order_by("-createtime")

        serializer = RecruitSerializer(recruit, many=True)
        return Response(serializer.data)
